<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

	class StationController extends AdminBaseController
	{

		/*园区房间列表页*/	
		public function index(){
	
		$incubator_type = $this->request->param('incubator_type',1);
		// echo $incubator_type;
		// exit;

		if($incubator_type==1){
			$where['s.incubator_type'] =1;	
		}else{
			$where['s.incubator_type'] =2;	
		}		
					
		/**搜索条件**/
		$station_name = $this->request->param('station_name');
		$address = $this->request->param('address');

		if ($station_name) {
			$where['station_name'] = ['like', "%$station_name%"];
		}

		if ($address) {
			$where['i.address'] = ['like', "%$address%"];
		}
		if(session('incubator_id')){
			$where['incubator_id']=session('incubator_id');
		}
		// var_dump($where);
		// exit;
		$where['s.status']=1;
		$list=Db::name('station')->alias('s')->where($where)->join('incubator i','i.id=s.incubator_id')->field('s.id,s.station_name,s.covered_area,s.price,s.is_use,i.address,s.tel,s.space_num,incubator_id')->order('id','desc')->paginate(5);
		// echo Db::name('station')->getLastSql();
		// exit;
			//$list=Db::name('station')->alias('a')->join('cx_incubator w','w.id=a.incubator_id')->paginate(5);
		$page=$list->render();
		
		$this->assign('station_name',$station_name);
		$this->assign('address',$address);
		$this->assign('incubator_type',1);
		$this->assign("page", $page);

		$this->assign("list", $list);
			 //  var_dump($list[0]);
			 // exit;
			// exit;
		return $this->fetch();
		}

		public function roomIndex(){

			$incubator_type = $this->request->param('incubator_type',2);
		// echo $incubator_type;
		// exit;
			$type = $this->request->param('type',0);		
			if($type){
				$where['type']=$type;
			}
			$this->assign('type',$type);	
			if($incubator_type==1){
				$where['i.incubator_type'] =1;	
			}else{
				$where['i.incubator_type'] =2;	
			}		

			/**搜索条件**/
			$station_name = $this->request->param('station_name');
			$address = $this->request->param('address');

			if ($station_name) {
				$where['station_name'] = ['like', "%$station_name%"];
			}

			if ($address) {
				$where['i.address'] = ['like', "%$address%"];
			}
			if(session('incubator_id')){
				$where['incubator_id']=session('incubator_id');
			}
			 $where['s.status']=1;
			// var_dump($where);
			// exit;
			$list=Db::name('station')->alias('s')->where($where)->join('incubator i','i.id=s.incubator_id')->field('s.id,s.station_name,s.covered_area,s.price,s.status,i.address,s.tel,s.pic,s.space_num,s.is_use,s.type,incubator_id')->order('id','desc')->paginate(5);
			//$list=json_decode(json_encode($list),true);
			// var_dump($list);
			// exit;
			//$list=Db::name('station')->alias('a')->join('cx_incubator w','w.id=a.incubator_id')->paginate(5);
			$page=$list->render();
			$this->assign('station_name',$station_name);
			$this->assign('address',$address);
			$this->assign('incubator_type',2);
			$this->assign("page", $page);

			$this->assign("list", $list);
			 //  var_dump($list[0]);
			 // exit;
			// exit;
			return $this->fetch();
		}


		/*园区添加*/		
		public function addStation(){			
			$cate = $this->request->param('cate');
			// echo $cate;
			// exit;
			//  echo $cate;
			// exit;

			if($cate==1){
				$incubator_type=1;
			}else{
				$incubator_type=2;
			}	
			if($_POST){
				$cate=$_POST['cate'];
				$data['incubator_type']=$cate;
				 // exit;
				unset($_POST['cate']);
				if($data['incubator_type']==1){
					$data['type']=1;
				}else{
					$data['type'] = $this->request->param('type',1);
				}
			// 		var_dump($_POST);
			// exit;
				// $cate = $this->request->param();
				// var_dump($cate);
				// exit;
				$data['incubator_id'] = $this->request->param('incubator_id',0,'intval');
				//$data['type'] = $this->request->param('type',1);
				$data['station_name'] = $this->request->param('station_name','');
				$data['price'] = $this->request->param('price');
				$data['pic'] = $this->request->param('pic','');

				$data['station_desc'] = $this->request->param('station_desc','');
				$data['station_type'] = $this->request->param('station_type',0,'intval');
				$data['tel'] = $this->request->param('tel','');
				$data['sort'] = $this->request->param('sort',0);
				$data['covered_area'] = $this->request->param('covered_area',0);
				$data['space_num'] = $this->request->param('space_num',0);
				$data['usable_area'] = $this->request->param('usable_area',0);	
				$data['is_rent'] = $this->request->param('is_rent',0);
				$data['station_num'] = $this->request->param('station_num',1);
				$data['is_use'] = $this->request->param('status',1);
				
			// echo $incubator_name ;
			// if($type_id==1){
			// 	$station_	
			// }					

			// echo $type_id;
				// var_dump($data);
				// exit;
				$add=Db::name('station')->insert($data);
				if($add){
					if($cate==1){
						$this->success('添加成功',url('Station/index'));
					}else{
						$this->success('添加成功',url('Station/roomIndex'));
					}
					
				}else{
					$this->error('添加失败');
				}	
			}else{

				$where=array();	
				if(session('incubator_id')){
					
					$where['id']=session('incubator_id');
				}

				$incubator_list=db('incubator')->where(array('status'=>1,'incubator_type'=>$incubator_type))->where($where)->field('id,incubator_name')->select();
				

				$this->assign('cate',$cate);
				$this->assign('incubator_list',$incubator_list);
				return $this->fetch();
			}
			





		}
		/*详情*/
		public function detailStation(){
			$cate = $this->request->param('cate');
			if($cate==1){
				$incubator_type=1;
			}else{
				$incubator_type=2;
			}	
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['cx_station.id']= $id;
			}else{
				// dump(info_back('','id为空',false));
				// exit;
				return info_back('','id为空',false);
			}
			$detail= Db::name('station')->where($where)->join('incubator','cx_station.incubator_id=cx_incubator.id')->find();
			//dump($detail);
			//echo Db::name('station')->getLastSql();
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}
			$incubator_list=db('incubator')->where(array('status'=>1,'incubator_type'=>$incubator_type))->where($where)->field('id,incubator_name')->select();
			$this->assign('incubator_list',$incubator_list);
			$this->assign('incubator_type',$cate);
			$this->assign('detail',$detail);
			return $this->fetch();
		}

		/*编辑园区*/	
		public function editStation(){

			$cate = $this->request->param('cate');
			// var_dump($cate);
			// exit;
			if($_POST){
			

				$cate = $_POST['cate'];
				$_POST['incubator_type']=$cate;
				unset($_POST['cate']);
				unset($_POST['address']);
				//var_dump($_POST);
				$edit = Db::name('station')->update($_POST);
				//if($edit){
					if($cate==2){
						$this->success('编辑成功',url('Station/roomIndex'));
					}else{
						$this->success('编辑成功',url('Station/index'));
					}
					
				//}
			}
			$cate = $this->request->param('cate');
			$where=array();
			$id= $this->request->param('id',1,'intval');
			if($id){
				$where['s.id']= $id;
			}else{
				$this->error('该信息id不能为空');
			}
			$detail= Db::name('station')->alias('s')->join('incubator i','s.incubator_id=i.id' )->where($where)->field('s.incubator_id,s.id,s.type,s.station_name,s.price,s.pic,s.station_desc,s.station_type,s.type,s.sort,s.covered_area,s.usable_area,s.is_rent,s.station_num,s.status,i.address,s.tel,s.space_num,s.is_use')->find();
			//dump($detail);
			//dump($detail);
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}
			$incubator_list=db('incubator')->where('status',1)->where($where)->field('id,incubator_name')->select();
			$this->assign('incubator_type',$cate);
			$this->assign('incubator_list',$incubator_list);
			$this->assign('detail',$detail);
			// var_dump($detail);
			// exit;
			return $this->fetch();

		}


		/*删除园区*/
		public function delStation(){
			$data['id']= $this->request->param('id',0,'intval');

			$data['status']=0;

			$del=Db::name('Station')->update($data);
			if($del){
				return 1;
			}else{
				return 0;
			}

		}	








































}